package com.ruyuan.eshop.seller.controller.seller.trade;

import com.ruyuan.eshop.common.domain.vo.*;
import com.ruyuan.eshop.framework.context.user.UserContext;
import com.ruyuan.eshop.framework.context.user.model.Seller;
import com.ruyuan.eshop.framework.core.WebPage;
import com.ruyuan.eshop.common.enums.OrderPermission;
import com.ruyuan.eshop.common.domain.dto.OrderDetailQueryParam;
import com.ruyuan.eshop.common.domain.dto.OrderQueryParam;
import com.ruyuan.eshop.common.enums.PaymentTypeEnum;
import com.ruyuan.eshop.common.service.trade.order.OrderOperateService;
import com.ruyuan.eshop.common.service.trade.order.OrderQueryService;
import com.ruyuan.eshop.seller.domain.query.OrderListQuery;
import com.ruyuan.eshop.seller.domain.request.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 商家订单控制器
 *
 * @author pwd
 * @version 1.0
 */

@Api(description = "商家订单API")
@RestController
@RequestMapping("/api/v1.0/trade/orders")
@Validated
public class OrderSellerController {

    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    private OrderOperateService orderOperateService;

    /**
     * 查询会员订单列表
     * @param orderListQuery
     * @return
     */
    @ApiOperation(value = "查询会员订单列表")
    @GetMapping
    public WebPage<OrderLineVO> list(@Validated OrderListQuery orderListQuery) {

        Seller seller = UserContext.getSeller();
        OrderQueryParam param = new OrderQueryParam();
        param.setOrderSn(orderListQuery.getOrderSn());
        param.setBuyerName(orderListQuery.getBuyerName());
        param.setGoodsName(orderListQuery.getGoodsName());
        param.setStartTime(orderListQuery.getStartTime());
        param.setEndTime(orderListQuery.getEndTime());
        param.setTag(orderListQuery.getOrderStatus());
        param.setPageNo(orderListQuery.getPageNo());
        param.setPageSize(orderListQuery.getPageSize());
        param.setSellerId(seller.getSellerId());
        param.setKeywords(orderListQuery.getKeywords());
        param.setClientType(orderListQuery.getClientType());

        WebPage<OrderLineVO> page = this.orderQueryService.list(param);
        return page;
    }

    /**
     * 查询单个订单明细
     * @param orderSn
     * @return
     */
    @ApiOperation(value = "查询单个订单明细")
    @GetMapping(value = "/{order_sn}")
    public OrderDetailVO get(@PathVariable("order_sn") String orderSn) {
        Seller seller = UserContext.getSeller();
        OrderDetailQueryParam queryParam = new OrderDetailQueryParam();
        queryParam.setSellerId(seller.getSellerId());
        OrderDetailVO detailVO = this.orderQueryService.getModel(orderSn, queryParam);

        //在线支付的订单商家不允许确认收款
        if (PaymentTypeEnum.ONLINE.value().equals(detailVO.getPaymentType())) {
            detailVO.getOrderOperateAllowableVO().setAllowPay(false);
        }

        return detailVO;
    }

    /**
     * 查询订单状态的数量
     * @return
     */
    @ApiOperation(value = "查询订单状态的数量")
    @GetMapping(value = "/status-num")
    public OrderStatusNumVO getStatusNum() {
        Seller seller = UserContext.getSeller();
        OrderStatusNumVO orderStatusNumVO = this.orderQueryService.getOrderStatusNum(null, seller.getSellerId());
        return orderStatusNumVO;
    }

    /**
     * 订单发货
     * @param orderSn
     * @param orderShipRequest
     * @return
     */
    @ApiOperation(value = "订单发货")
    @PostMapping(value = "/{order_sn}/delivery")
    public boolean ship(@PathVariable(name = "order_sn") String orderSn, @RequestBody OrderShipRequest orderShipRequest) {
        Seller seller = UserContext.getSeller();
        DeliveryVO delivery = new DeliveryVO();
        delivery.setDeliveryNo(orderShipRequest.getShipNo());
        delivery.setOrderSn(orderSn);
        delivery.setLogiId(orderShipRequest.getLogiId());
        delivery.setLogiName(orderShipRequest.getLogiName());
        delivery.setOperator("店铺:" + seller.getSellerName());
        orderOperateService.ship(delivery, OrderPermission.seller);
        return true;
    }

    /**
     * 商家修改收货人地址
     * @param orderSn
     * @param updateOrderConsigneeRequest
     * @return
     */
    @ApiOperation(value = "商家修改收货人地址")
    @PutMapping(value = "/{order_sn}/address")
    public OrderConsigneeVO updateOrderConsignee(@ApiIgnore @PathVariable(name = "order_sn") String orderSn,
                                                 @RequestBody UpdateOrderConsigneeRequest updateOrderConsigneeRequest) {
        OrderConsigneeVO orderConsignee = new OrderConsigneeVO();
        orderConsignee.setOrderSn(orderSn);
        orderConsignee.setShipName(updateOrderConsigneeRequest.getShipName());
        orderConsignee.setRemark(updateOrderConsigneeRequest.getRemark());
        orderConsignee.setShipAddr(updateOrderConsigneeRequest.getShipAddr());
        orderConsignee.setShipMobile(updateOrderConsigneeRequest.getShipMobile());
        orderConsignee.setShipTel(updateOrderConsigneeRequest.getShipTel());
        orderConsignee.setReceiveTime(updateOrderConsigneeRequest.getReceiveTime());
        orderConsignee = this.orderOperateService.updateOrderConsignee(orderConsignee);
        return orderConsignee;
    }

    /**
     * 商家修改订单价格
     * @param orderSn
     * @param updateOrderPriceRequest
     * @return
     */
    @ApiOperation(value = "商家修改订单价格")
    @PutMapping(value = "/{order_sn}/price")
    public String updateOrderPrice(@PathVariable(name = "order_sn") String orderSn, @RequestBody UpdateOrderPriceRequest updateOrderPriceRequest) {
        this.orderOperateService.updateOrderPrice(orderSn, updateOrderPriceRequest.getOrderPrice());
        return "";
    }

    /**
     * 确认收款
     * @param orderSn
     * @param payOrderRequest
     * @return
     */
    @ApiOperation(value = "确认收款")
    @PostMapping(value = "/{order_sn}/pay")
    public String payOrder(@ApiIgnore @PathVariable("order_sn") String orderSn, @RequestBody PayOrderRequest payOrderRequest) {
        this.orderOperateService.payOrder(orderSn, payOrderRequest.getPayPrice(), "", OrderPermission.seller);
        return "";
    }

    /**
     * 取消订单
     * @param orderSn
     * @param cancelOrderRequest
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PostMapping(value = "/{order_sn}/cancel")
    public String cancel(@ApiIgnore @PathVariable("order_sn") String orderSn, @RequestBody CancelOrderRequest cancelOrderRequest) {
        CancelVO cancelVO = new CancelVO();
        cancelVO.setOperator("商家取消");
        cancelVO.setOrderSn(orderSn);
        cancelVO.setReason(cancelOrderRequest.getReason());
        orderOperateService.cancel(cancelVO, OrderPermission.seller);
        return "";
    }

}
